#coding=utf-8
#Boa:FramePanel:PanelRouteList

import wx

from MyListCtrl import *
from wx.lib.splitter import MultiSplitterWindow

[wxID_PANELROUTELIST, wxID_PANELROUTELISTBTNADD, 
 wxID_PANELROUTELISTBTNREGIONNOTSEND, wxID_PANELROUTELISTBTNREGIONSEND, 
 wxID_PANELROUTELISTBTNSHOWALLCUSTOMER, wxID_PANELROUTELISTBUTTON_ALL_REGION, 
 wxID_PANELROUTELISTCHECKBOX_ALARM_CUSTOMER, 
 wxID_PANELROUTELISTCHECKBOX_ONLY_SHOP, 
] = [wx.NewId() for _init_ctrls in range(8)]

class PanelRouteList(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELROUTELIST, name=u'PanelRouteList',
              parent=prnt, pos=wx.Point(708, 216), size=wx.Size(916, 654),
              style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(900, 616))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.btnAdd = wx.Button(id=wxID_PANELROUTELISTBTNADD,
              label=u'\u65b0\u589e\u8def\u7ebf', name=u'btnAdd', parent=self,
              pos=wx.Point(8, 8), size=wx.Size(93, 26), style=0)
        self.btnAdd.Bind(wx.EVT_BUTTON, self.OnBtnAddButton,
              id=wxID_PANELROUTELISTBTNADD)

        self.button_all_region = wx.Button(id=wxID_PANELROUTELISTBUTTON_ALL_REGION,
              label=u'\u5168\u90e8\u533a\u57df', name=u'button_all_region',
              parent=self, pos=wx.Point(8, 258), size=wx.Size(144, 26),
              style=0)
        self.button_all_region.Bind(wx.EVT_BUTTON,
              self.OnButton_all_regionButton,
              id=wxID_PANELROUTELISTBUTTON_ALL_REGION)

        self.checkBox_only_shop = wx.CheckBox(id=wxID_PANELROUTELISTCHECKBOX_ONLY_SHOP,
              label=u'\u53ea\u663e\u793a\u4e94\u91d1\u5e97\u5ba2\u6237',
              name=u'checkBox_only_shop', parent=self, pos=wx.Point(460, 262),
              size=wx.Size(168, 16), style=0)
        self.checkBox_only_shop.SetValue(True)
        self.checkBox_only_shop.Bind(wx.EVT_CHECKBOX,
              self.OnCheckBox_only_shopCheckbox,
              id=wxID_PANELROUTELISTCHECKBOX_ONLY_SHOP)

        self.checkBox_alarm_customer = wx.CheckBox(id=wxID_PANELROUTELISTCHECKBOX_ALARM_CUSTOMER,
              label=u'\u8b66\u62a5\u5ba2\u60c5',
              name=u'checkBox_alarm_customer', parent=self, pos=wx.Point(624,
              264), size=wx.Size(105, 16), style=0)
        self.checkBox_alarm_customer.SetValue(False)
        self.checkBox_alarm_customer.Bind(wx.EVT_CHECKBOX,
              self.OnCheckBox1Checkbox,
              id=wxID_PANELROUTELISTCHECKBOX_ALARM_CUSTOMER)

        self.btnRegionNotSend = wx.Button(id=wxID_PANELROUTELISTBTNREGIONNOTSEND,
              label=u' \u8def\u7ebf\u5916', name=u'btnRegionNotSend',
              parent=self, pos=wx.Point(160, 258), size=wx.Size(104, 26),
              style=0)
        self.btnRegionNotSend.Bind(wx.EVT_BUTTON, self.OnBtnRegionNotSendButton,
              id=wxID_PANELROUTELISTBTNREGIONNOTSEND)

        self.btnRegionSend = wx.Button(id=wxID_PANELROUTELISTBTNREGIONSEND,
              label=u'\u8def\u7ebf\u5185', name=u'btnRegionSend', parent=self,
              pos=wx.Point(272, 258), size=wx.Size(93, 26), style=0)
        self.btnRegionSend.Bind(wx.EVT_BUTTON, self.OnBtnRegionSendButton,
              id=wxID_PANELROUTELISTBTNREGIONSEND)

        self.btnShowAllCustomer = wx.Button(id=wxID_PANELROUTELISTBTNSHOWALLCUSTOMER,
              label=u'\u663e\u793a\u8def\u7ebf\u5ba2\u6237',
              name=u'btnShowAllCustomer', parent=self, pos=wx.Point(736, 256),
              size=wx.Size(112, 26), style=0)
        self.btnShowAllCustomer.Bind(wx.EVT_BUTTON,
              self.OnBtnShowAllCustomerButton,
              id=wxID_PANELROUTELISTBTNSHOWALLCUSTOMER)

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.lcRoute = MyListCtrlRoute(self)
        self.lcRoute.SetSize(wx.Size(1000, 210))
        self.lcRoute.Move(wx.Point(8, 40))
        self.lcRoute.refresh()
        self.lcRoute.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnRouteItemSelected)
        
        self.splitterWin = MultiSplitterWindow(self, style=wx.SP_LIVE_UPDATE)
        self.lcRegion = MyListCtrlRouteDetail(self.splitterWin)
        self.lcRegion.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnRegionItemSelected)
        self.lcCustomer = MyCustomerStatListCtrl(self.splitterWin) #MyCustomerListCtrl(self.splitterWin)

        self.lcRegion.SetMinSize(wx.Size(100, 100))
        self.splitterWin.AppendWindow(self.lcRegion, 200)

        self.lcCustomer.SetMinSize(wx.Size(100, 100))
        self.splitterWin.AppendWindow(self.lcCustomer)

        self.splitterWin.Move(wx.Point(10, 290))
        # 设置大小为什么必须放在最后？放在AppendWindow之前导致sashPos参数不起作用
        self.splitterWin.SetSize(wx.Size(1000, 400))
        self.splitterWin.SetOrientation(wx.HORIZONTAL)
        self.splitterWin.SizeWindows()

        # self.lcRegion = MyListCtrlRouteDetail(self)
        # self.lcRegion.Move(wx.Point(8, 290))
        # self.lcRegion.SetSize(wx.Size(280, 400))
        self.lcRegion.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnRegionItemSelected)
        self.lcRegion.refresh()

        # self.lcCustomer = MyCustomerListCtrl(self)
        # self.lcCustomer.Move(wx.Point(300, 290))
        # self.lcCustomer.SetSize(wx.Size(900, 400))

        self.button_all_region.SetLabel("ALL ("+str(self.lcRegion.m_active_customer_count)+"/"+str(self.lcRegion.m_customer_count)+")")
        
    def OnBtnAddButton(self, event):
        import DlgRouteEditor
        
        dlg = DlgRouteEditor.create(self)
        if (dlg.ShowModal() != wx.ID_OK):
            return
        
        self.lcRoute.refresh()

    def OnButton_all_regionButton(self, event):
        self.lcRegion.refresh()
    
    def OnRouteItemSelected(self, event):
    
        item = self.lcRoute.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        routeId = int(self.lcRoute.GetItem(item, self.lcRoute.iId).GetText())

        self.lcRegion.refresh({'routeId':routeId})
    
    def OnRegionItemSelected(self, event):
        item = self.lcRegion.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if item >= 0:
            regionId = self.lcRegion.getRegionId(item)
            self.lcCustomer.refresh({
                'regionId':regionId,
                'only_wujindian': self.checkBox_only_shop.GetValue(),
                'only_alarm_customer': self.checkBox_alarm_customer.GetValue(),
            })

    def OnCheckBox_only_shopCheckbox(self, event):
        self.OnRegionItemSelected(event)

    def OnCheckBox1Checkbox(self, event):
        self.OnRegionItemSelected(event)

    def OnBtnRegionNotSendButton(self, event):
        self.lcRegion.refresh({'routeId':-1})

    def OnBtnRegionSendButton(self, event):
        self.lcRegion.refresh({'routeId':-2})

    def OnBtnShowAllCustomerButton(self, event):
        item = self.lcRoute.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        routeId = int(self.lcRoute.GetItem(item, self.lcRoute.iId).GetText())

        self.lcCustomer.refresh({
                'routeId':routeId,
                'only_wujindian': self.checkBox_only_shop.GetValue(),
                'only_alarm_customer': self.checkBox_alarm_customer.GetValue(),
            })
